package com.minivision.sdktestdemo.ui.custom;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/3.
 */
public class LrcProviderImpl implements LrcScrollView.LrcProvider, LrcLoader {
    private Context mContext;
    private LrcLoader mLoader;
    private List<String> mLrcList;
    private List<Long> mLrcTimes;

    public LrcProviderImpl(Context context) {
        mContext = context;
        mLrcList = new ArrayList<>();
        mLrcTimes = new ArrayList<>();
    }

    public void queryNewLrc(String name, Action1<String> action) {
        Observable.just(name)
                  .map(new Func1<String, String>() {
                      @Override
                      public String call(String s) {
                          String content = getLrcText(s);
                          readContent(content);
                          return content;
                      }
                  })
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(action);
    }

    private synchronized void readContent(String content) {
        mLrcTimes.clear();
        mLrcList.clear();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new StringReader(content));
            String line = null;
            while ((line = reader.readLine()) !=  null) {
                int start = line.indexOf('[');
                int end = line.lastIndexOf(']');
                if (start != -1 && end != -1) {
                    mLrcList.add(line.substring(end + 1));
                    mLrcTimes.add(getMillSeconds(line.substring(start + 1, end)));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private long getMillSeconds(String format) {
        int start = format.indexOf(':');
        int end = format.lastIndexOf('.');
        long mimute = Long.parseLong(format.substring(0, start));
        long second = Long.parseLong(format.substring(start + 1, end));
        long millSecond = Long.parseLong(format.substring(end + 1));
        return mimute * 60l * 1000l + second * 1000l + millSecond;
    }

    @Override
    public List<String> getLrcList() {
        return mLrcList;
    }

    @Override
    public List<Long> getLrcTimeList() {
        return mLrcTimes;
    }

    @Override
    public String getLrcText(String name) {
        if (mLoader != null) {
            return mLoader.getLrcText(name);
        }
        return "";
    }

    @Override
    public String getErrorMsg() {
        if (mLoader != null) {
            return mLoader.getErrorMsg();
        }
        return "加载歌词失败";
    }

    @Override
    public LrcLoader setLrcLoader(LrcLoader loader) {
        mLoader = loader;
        return this;
    }
}
